﻿// -----------------------------------------------------------------------
// <copyright file="ColumnValue.cs" company="JSC Centido">
//  Copyright (c) JSC Centido. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace Centido.Core
{
    using System;

    /// <summary>
    ///   A table column value.
    /// </summary>
    /// <remarks>This is a helper class to use when selecting, inserting and updating rows with
    /// the methods of the <see cref="Centido.Core.Table"/> class.</remarks>
    /// <example>
    ///   The following example shows how to insert, select and update rows using this helper class.
    /// <code>
    ///     using System;
    ///     using Centido.Core;
    /// 
    ///     class Sample
    ///     {
    ///         static void Main()
    ///         {
    ///             var domainConfig = new DomainConfiguration()
    ///             {
    ///                 DbConnectionString = @"Driver={SQL Server};Server=.\SQLEXPRESS;Database=MyDb;Uid=admin;Pwd=P@ssw0rd;",
    ///                 DbProviderName = "MSSQLSERVER"
    ///             };
    ///         
    ///             var domain = Domain.Build(domainConfig);
    ///         
    ///             domain.DeleteAll();
    ///             
    ///             //Adding a new table
    ///             var table = domain.AddTable("students");
    ///             
    ///             //Adding some table columns
    ///             var nameColumn = table.AddColumn("name", ColumnType.String) as StringColumn;
    ///             nameColumn.IsNotNull = true;
    ///             nameColumn.IsNotEmpty = true;
    ///             nameColumn.MaxLength = 64;
    /// 
    ///             var birthdayColumn = table.AddColumn("birthday", ColumnType.DateTime) as DateTimeColumn;
    ///             birthdayColumn.IsNotNull = true;
    /// 
    ///             //Populating the table with data
    ///             table.GrantAll();
    ///
    ///             var id = table.InsertRow(nameColumn.Value("John Smith"), birthdayColumn.Value(DateTime.Now.AddYears(-30)));
    ///             
    ///             //Reading the inserted values
    ///             var surname = nameColumn.EmptyValue();
    ///             var birthday = birthdayColumn.EmptyValue();
    /// 
    ///             table.ReadRow(id, surname, birthday);
    /// 
    ///             Console.WriteLine("After insert: surname = {0}, birthday = {1}", surname.GetString(), birthday.GetDateTime());
    /// 
    ///             //Updating the surname and the birthday of the student
    ///             table.UpdateRow(id, nameColumn.Value("Mary Sue"), birthdayColumn.Value(DateTime.Now.AddYears(-15)));
    /// 
    ///             //Reading the updated values
    ///             surname = nameColumn.EmptyValue();
    ///             birthday = birthdayColumn.EmptyValue();
    /// 
    ///             table.ReadRow(id, surname, birthday);
    /// 
    ///             Console.WriteLine("After update: surname = {0}, birthday = {1}", surname.GetString(), birthday.GetDateTime());
    ///         }
    ///     }
    /// </code>
    /// </example>
    public sealed class ColumnValue
    {
        /// <summary>
        ///   The table column.
        /// </summary>
        private readonly Column _column;

        /// <summary>
        ///   Initializes a new instance of the <see cref = "Centido.Core.ColumnValue" /> class
        ///   for the specified table column id and table column value.
        /// </summary>
        /// <param name = "column">The parent table column.</param>
        /// <param name = "value">The table column value.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="column"/> is null.</exception>
        internal ColumnValue(Column column, object value)
        {
            if (column == null)
                throw new ArgumentNullException("column");

            _column = column;
            Value = value;
        }

        /// <summary>
        ///   Gets the parent table column.
        /// </summary>
        public Column Column
        {
            get { return _column; }
        }

        /// <summary>
        ///   Gets the parent column name.
        /// </summary>
        public string ColumnName
        {
            get
            {
                return Column.Name;
            }
        }

        /// <summary>
        ///   Gets or sets the column value.
        /// </summary>
        public object Value 
        { 
            get;
            set;
        }


        /// <summary>
        ///   Returns true, if the column value is not empty; otherwise, returns false.
        /// </summary>
        public bool HasValue
        {
            get
            {
                return Value != null && Value != DBNull.Value;
            }
        }
    }
}